$(function () {
    $("#jqGrid").jqGrid({
        url: '../dictwagestype/listXtd',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', width: 50, key: true,hidden: true },
			{ label: '名称', name: 'name', width: 80 }, 			
			{ label: '岗位id', name: 'jobid', width: 40,hidden:true }, 	
			{ label: '岗位', name: 'jobName', width: 40 },
			{ label: '结算方式（0：固定薪资，1：业绩提成）', name: 'statementstype', width: 140, formatter: function(value, options, row){
				return value === 0 ? 
						'<span class="label label-primary">固定薪资</span>' : 
						'<span class="label label-info">业绩提成</span>';
				}}, 			
			{ label: '固定薪资', name: 'fixedpay', width: 60 }, 			
			{ label: '排序', name: 'orderno', width: 40}, 			
//			{ label: '备注字段', name: 'excol', width: 80 }, 			
			{ label: '备注', name: 'memo', width: 80 }, 			
			{ label: '创建时间', name: 'createtime', width: 80,hidden: true }, 			
			{ label: '创建人', name: 'createby', width: 80,hidden: true }, 			
			{ label: '修改时间', name: 'updatetime', width: 80,hidden: true }, 			
			{ label: '修改人', name: 'updateby', width: 80,hidden: true }, 			
			{ label: '状态', name: 'state', width: 60, formatter: function(value, options, row){
				return value === false ? 
						'<span class="label label-danger">禁用</span>' : 
						'<span class="label label-success">正常</span>';
				}},			
        ],
		viewrecords: true,
		height: $(window).height()-120,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		dictWagesType: {},
		one:"",
		two:"",
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";			
			vm.dictWagesType = {statementstype:1,state:true};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.dictWagesType.id == null ? "../dictwagestype/saveXtd" : "../dictwagestype/updateXtd";
			if(!(vm.one==""&&vm.two==""&&vm.dictWagesType.name!=""&&vm.dictWagesType.name!=null)){
    			alert("输入的内容存在问题，无法保存！");
    			return  			
    		}
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.dictWagesType),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../dictwagestype/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../dictwagestype/infoXtd/"+id, function(r){
                vm.dictWagesType = r.dictWagesType;
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.one='';
			vm.two='';
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				page:1,
            }).trigger("reloadGrid");
		},
		idNameValid:function(name){ 
			name = $("#name").val();   
			var reg = /[\u4e00-\u9fa5]/;
			if(name==""||name==null){
			 	   vm.one="名称不能为空！";
			    }else if(reg.test(name)){
			    vm.one="";
			    }
			     else{
			           vm.one="名称输入不合法!";
			    }
		},
		chooseJob:function(){
			var arrayNewList = new Array();
			var columnCont = new Array();
			$('#zjJobId').jqGrid({
    	        url: '../dictjob/list',
    	        datatype: "json",
    	        colModel: [			
    	        	{ label: 'id', name: 'id',hidden:true, width: 50, key: true },
    				{ label: '编号', name: 'jobno', width: 80 }, 
    				{ label: '名称', name: 'name', width: 80 }, 			   							
    				{ label: '排序', name: 'orderno', width: 80 }, 					
    				{ label: '备注', name: 'memo', width: 80 }, 						
    				{ label: '状态', name: 'state', width: 80, formatter: function(value, options, row){
    					return value === false ? 
    							'<span class="label label-danger">禁用</span>' : 
    							'<span class="label label-success">正常</span>';
    					} }	
    	        ],
    	        viewrecords: true,
		        height: window.screen.availHeight-250,
		        rowNum: 10,
				rowList : [10,30,50],
		        rownumbers: true, 
		        rownumWidth: 50, 
		        width:window.screen.availWidth-250,  
		        autowidth: false,  
		        shrinkToFit: true,
		        multiselect: true,
    	        pager: "#jqGridPagercf",
    	        jsonReader : {
    	            root: "page.list",
    	            page: "page.currPage",
    	            total: "page.totalPage",
    	            records: "page.totalCount"
    	        },
    	        prmNames : {
    	            page:"page", 
    	            rows:"limit", 
    	            order: "order"
    	        },
    	        onSelectAll:function(aRowids,status){
		            if(status==true){
		                //循环aRowids数组，将code放入arrayNewList数组中
		                $.each(aRowids,function(i,item){
		                   saveData(item);
		                })
		            }else{
		                //循环aRowids数组，将code从arrayNewList中删除
		                $.each(aRowids,function(i,item){
		                   deleteIndexData(item);
		                })
		            }

		        },
		        onSelectRow:function(aRowids,status){
		            if(status==true){
		               saveData(aRowids);
		            }else{
		               deleteIndexData(aRowids);
		            }

		        },
    	        gridComplete:function(){
    	        	//隐藏grid底部滚动条
    	        	$("#zjJobId").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
    	        	layer.open({
    	        		type : 1,
//    	        		offset:'50px',
    	        		title : "岗位列表",
    	        		closeBtn :1,  
    	        		maxmin:1,
    	        		btn : [ '确认', '取消' ],
    	        		area : [ "100%", "100%" ],
    	        		skin : 'layui-layer-molv', // 
    	        		shade:0,
    	        		shadeClose : false,
    	        		content : $('#zjJobName'),
    	        		cancel: function(index, layero){ 
    	        			arrayNewList=[];
		        			columnCont=[];
    	        			layer.close(index)
    	        			  
    	        			},
    	    			
    	        		yes:function(index,layero){
    	        			if(arrayNewList.length==0){
		        				alert('请选择一条记录');
		        				return ;
		        			}
		        			if(arrayNewList.length>1){
		        				alert('只能选择一条记录');
		        				return ;
		        			}
		        		    var entrailsid=arrayNewList.join(",")
		        			var entrailsname=columnCont.join(",");
		        			
		        			if(arrayNewList==undefined){
		        				vm.dictWagesType.jobName="";
		        				
		        			}else{
		        				vm.dictWagesType.jobName=entrailsname;
		        				
		        			}
		        			if(columnCont==undefined){
		        				vm.dictWagesType.jobid="";
		        				
		        			}else{
		        				vm.dictWagesType.jobid=entrailsid;
		        				
		        			}
		        			$('#jobName').val(entrailsname);	
		        			if(!($("#jobName").val()==""||$("#jobName").val()==null)){	        			    	        				
	    	        			   vm.two="";
	    	        		}
		        			if(arrayNewList.length!=0){
		        				arrayNewList=[];
		        				columnCont=[];
		        			layer.close(index); 
		        			}
    	        		  },
    	        		  btn2:function (index,layero){ 
      	        			if($("#jobName").val()==""||$("#jobName").val()==null){	        			    	        				
      	        			   vm.two="岗位不能为空！";
      	        			} 
      	        			arrayNewList=[];
  		        			columnCont=[];
      	        			layer.close(index)
      	        			  
      	        		  }
    	        	});
    	        }
    	    });
			function saveData(obj){
				var rowData = $("#zjJobId").jqGrid("getRowData", obj);
	            arrayNewList.push(rowData.id);
	            columnCont.push(rowData.name);
	        }
			function RemoveValByIndex(arr, index) {
				  arr.splice(index, 1);
				}
	        function deleteIndexData(obj){
	        	var rowData = $("#zjJobId").jqGrid("getRowData", obj);
	            //获取obj在arrayNewList数组中的索引值
	            for(i = 0; i < arrayNewList.length; i++){
	                if (arrayNewList[i] == rowData.id){
	                    //根据索引值删除arrayNewList中的数据
	                	RemoveValByIndex(arrayNewList, i);
	                	RemoveValByIndex(columnCont, i);
	                    i--;
	                }
	             }
	        }
	        $("#zjJobId").jqGrid('clearGridData');
			$("#zjJobId").trigger('reloadGrid');
		},
		checkJob:function(){
			department = $("#jobName").val();   
			var reg = /[\u4e00-\u9fa5]/;
			if(department==""||department==null){
		 	   vm.two="岗位不能为空！";
		 	 
		    }else if(reg.test(department)){
		    	vm.two="";
		    }
		     else{
		           vm.two="岗位输入不合法!";
		    }
		},
	}
});